package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysDeviceList;
import com.ruoyi.system.service.ISysDeviceListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 设备清单管理Controller
 * 
 * @author ljf
 */
@Api(tags = "设备清单管理")
@RestController
@RequestMapping("/system/devicelist")
public class SysDeviceListController extends BaseController
{
    @Autowired
    private ISysDeviceListService deviceListService;

    /**
     * 获取设备清单列表
     */
    @ApiOperation(value = "获取设备清单列表")
    @PreAuthorize("@ss.hasPermi('system:devicelist:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDeviceList deviceList)
    {
        startPage();
        List<SysDeviceList> list = deviceListService.selectDeviceList(deviceList);
        return getDataTable(list);
    }

    /**
     * 新增设备清单
     */
    @ApiOperation(value = "新增设备清单")
    @PreAuthorize("@ss.hasPermi('system:devicelist:add')")
    @Log(title = "设备清单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDeviceList deviceList)
    {
        deviceList.setCreateBy(getUsername());
        return toAjax(deviceListService.insertDeviceList(deviceList));
    }

    /**
     * 修改设备清单
     */
    @ApiOperation(value = "修改设备清单")
    @PreAuthorize("@ss.hasPermi('system:devicelist:edit')")
    @Log(title = "设备清单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDeviceList deviceList)
    {
        deviceList.setUpdateBy(getUsername());
        return toAjax(deviceListService.updateDeviceList(deviceList));
    }

    /**
     * 删除设备清单
     */
    @ApiOperation(value = "删除设备清单")
    @PreAuthorize("@ss.hasPermi('system:devicelist:remove')")
    @Log(title = "设备清单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{listIds}")
    public AjaxResult remove(@PathVariable Long[] listIds)
    {
        return toAjax(deviceListService.deleteDeviceListByIds(listIds));
    }

    /**
     * 设备清单数据导出
     * @author ljf
     * @date 2021-12-09 09:46:43
     */
    @ApiOperation(value = "导出设备清单数据")
    @Log(title = "设备清单", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:devicelist:export')")
    @GetMapping("/export")
    public AjaxResult export(SysDeviceList deviceList)
    {
        List<SysDeviceList> list = deviceListService.selectDeviceList(deviceList);
        ExcelUtil<SysDeviceList> util = new ExcelUtil<SysDeviceList>(SysDeviceList.class);
        return util.exportExcel(list, "设备清单数据");
    }

    /**
     * 设备清单数据导入
     * @author ljf
     * @date 2021-12-09 09:47:43
     */
    @ApiOperation(value = "导入设备清单数据")
    @Log(title = "设备清单", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:devicelist:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysDeviceList> util = new ExcelUtil<SysDeviceList>(SysDeviceList.class);
        List<SysDeviceList> deviceList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = deviceListService.importDeviceList(deviceList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 设备清单导入模板下载
     * @author ljf
     * @date 2021-12-09 10:22:15
     */
    @ApiOperation(value = "下载设备清单模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<SysDeviceList> util = new ExcelUtil<SysDeviceList>(SysDeviceList.class);
        return util.importTemplateExcel("设备清单数据");
    }
}
